// ignore_for_file: use_build_context_synchronously

import 'package:aitao_flutter/common_ui/authentication_window.dart';
import 'package:aitao_flutter/dynamic/controllers/dynamic_controller.dart';
import 'package:aitao_flutter/dynamic/follow_dynamic_page.dart';
import 'package:aitao_flutter/dynamic/latest_dynamic_page.dart';
import 'package:aitao_flutter/dynamic/mine_dynamic_page.dart';
import 'package:aitao_flutter/res/r.dart';
import 'package:aitao_flutter/routes/routes.dart';
import 'package:aitao_flutter/utils/log_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../const/common_const.dart';
import '../request/api.dart';
import '../request/requestManager.dart';
import '../style/color_style.dart';
import '../style/text_style.dart';

class DynamicPage extends StatefulWidget {
  DynamicPage({super.key});
  final DynamicController controller = Get.put(DynamicController());
  @override
  State<DynamicPage> createState() => _DynamicPageState();
}

class _DynamicPageState extends State<DynamicPage>
    with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
  late TabController tabController;
  final List<String> _tabList = [
    DynamicStringConst.latest,
    DynamicStringConst.focusText,
    DynamicStringConst.mineText
  ];
  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    tabController = TabController(
        length: 3, vsync: this, initialIndex: widget.controller.index);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return GetBuilder<DynamicController>(builder: ((controller) {
      return SafeArea(
          child: Scaffold(
        backgroundColor: ColorStyle.backGroundColor,
        body: Column(children: [
          Container(
            padding: const EdgeInsets.fromLTRB(14, 0, 14, 0),
            height: 44.h,
            color: ColorStyle.backGroundColor,
            child:
                Row(crossAxisAlignment: CrossAxisAlignment.center, children: [
              Expanded(
                  child: Container(
                alignment: Alignment.center,
                color: ColorStyle.backGroundColor,
                child: TabBar(
                  isScrollable: true,
                  // unselectedLabelStyle: const TextStyle(
                  //     color: ColorStyle.ashGreyColor, fontSize: 15),
                  labelColor: ColorStyle.normalTextColor,
                  indicatorColor: ColorStyle.themeColor,
                  indicatorSize: TabBarIndicatorSize.label,
                  indicatorPadding: const EdgeInsets.only(left: 8, right: 8),
                  indicatorWeight: 3,
                  labelPadding: const EdgeInsets.only(bottom: 5),
                  onTap: (index) {
                    controller.selectIndex(index);
                  },
                  tabs: _tabList.asMap().entries.map((e) {
                    return Padding(
                        padding: const EdgeInsets.only(left: 8, right: 8),
                        child: Text(e.value,
                            style: e.key == widget.controller.index
                                ? Styles.boldText_16
                                : const TextStyle(
                                    color: ColorStyle.normalTextColor,
                                    fontSize: 15)));
                  }).toList(),
                  controller: tabController,
                ),
              )),
              GestureDetector(
                child: Image.asset(
                  ImagePath.assetsDynamicAdd,
                  width: 30.w,
                  height: 30.w,
                ),
                onTap: () {
                  checkSendPermission();
                },
              )
            ]),
          ),
          Expanded(
              child: TabBarView(
            controller: tabController,
            children: const [
              LatestDynamicPage(),
              FollowDynamicPage(),
              MineDynamicPage(),
            ],
          ))
        ]),
      ));
    }));
  }

  void checkSendPermission() async {
    // 检测权限
    var result = await RequestManager().post(
      API.checkDynamicPermission,
    );
    if (result != null) {
      WJLogUtils.wjLog(result);

      if (result['code'] == 0) {
        Get.toNamed(Routes.sendDynamicPage);
      } else {
        showDialog(
            context: context,
            builder: (BuildContext context1) {
              return AuthenticationWindow(
                authenticationCallBack: () {
                  Navigator.pop(context1);
                  Get.toNamed(Routes.certificationPage);
                  // print('object1');
                },
              );
            });
      }
    }
  }
}
